package com.qianfeng.smartmeeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.smartmeeting.dto.TimeSlotDto;
import com.qianfeng.smartmeeting.entity.Booking;
import com.qianfeng.smartmeeting.entity.Room;
import com.qianfeng.smartmeeting.entity.TimeSlot;
import com.qianfeng.smartmeeting.mapper.BookingMapper;
import com.qianfeng.smartmeeting.mapper.RoomMapper;
import com.qianfeng.smartmeeting.mapper.TimeSlotMapper;
import com.qianfeng.smartmeeting.service.RoomService;
import com.qianfeng.smartmeeting.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Autowired
    private TimeSlotMapper timeSlotMapper;
    
    @Autowired
    private BookingMapper bookingMapper;

    @Override
    public ResponseResult<List<TimeSlotDto>> getAvailableSlots(Long roomId, LocalDate date) {
        try {
            // 1. 获取所有时间段
            List<TimeSlot> allSlots = timeSlotMapper.selectList(null);

            // 2. 获取该房间该日期的所有预约
            List<Booking> bookings = bookingMapper.selectList(
                    new LambdaQueryWrapper<Booking>()
                            .eq(Booking::getRoomId, roomId)
                            .eq(Booking::getBookingDate, date)
                            .in(Booking::getStatus, Arrays.asList(0, 1))
            );

            // 3. 转换为DTO并标记可用状态
            List<TimeSlotDto> result = allSlots.stream()
                    .map(slot -> {
                        TimeSlotDto dto = new TimeSlotDto();
                        dto.setSlotId(slot.getSlotId());
                        dto.setSlotName(slot.getSlotName());
                        dto.setStartTime(slot.getStartTime());
                        dto.setEndTime(slot.getEndTime());

                        // 检查是否被预约
                        boolean isBooked = bookings.stream()
                                .anyMatch(b -> b.getSlotId().equals(slot.getSlotId()));
                        dto.setAvailable(!isBooked);

                        return dto;
                    })
                    .collect(Collectors.toList());

            return ResponseResult.success(result);

        } catch (Exception e) {
            return ResponseResult.error("查询可用时间段失败: " + e.getMessage());
        }
    }

    @Override
    public ResponseResult<List<Room>> getAllRooms() {
        List<Room> rooms = list();
        return ResponseResult.success(rooms);
    }

    @Override
    public ResponseResult<Room> getRoomById(Long id) {
        Room room = getById(id);
        if (room == null) {
            return ResponseResult.error("会议室不存在");
        }
        return ResponseResult.success(room);
    }
}